package org.tsp.android.MapExample;

import java.util.Iterator;
import java.util.List;

import org.data.ListMarkersTresor;
import org.data.MarkerTresor;
import org.tsp.android.SensorExample.MyLocationListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MyMapActivity extends MapActivity {
	    
	private MapView m_mapView;
	private MapController mController;
	private MyItemizedOverlay images;
	private int lastIndiceMarkers;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_view);
        m_mapView = (MapView) findViewById(R.id.map_view);
        m_mapView.setBuiltInZoomControls(true);
                
        lastIndiceMarkers = -1;
        mController = m_mapView.getController();
        mController.setZoom(4);
        
        //creation de la liste des futur markers
        List<Overlay> map_overlays = m_mapView.getOverlays();
        images = new MyItemizedOverlay(this, getResources().getDrawable(R.drawable.blue_dot));
        map_overlays.add(images);

	    Iterator<?> i = ListMarkersTresor.getInstance().getListMarkerTresors().iterator();
	    while(i.hasNext()){
	    	MarkerTresor unTresor = (MarkerTresor) i.next();
	    	Drawable unMarker;
	    	if(unTresor.isActif())
	    	{
	    		unMarker = getResources().getDrawable(R.drawable.yellow_dot);
	    	}else{
	    		unMarker = getResources().getDrawable(R.drawable.blue_dot);	    		
	    	}
	    	OverlayItem unItemPosition = new OverlayItem(new GeoPoint( unTresor.getLatitude(), unTresor.getLongitude()), "Item position", "Item position description");   
			unMarker.setBounds(0,0,unMarker.getIntrinsicWidth(),unMarker.getIntrinsicHeight());
			unItemPosition.setMarker(unMarker);
	        images.addOverlay(unItemPosition);
	    }   
    }
 
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/*#1
	 * Ajout de la position par localisation
	 */
	private LocationManager m_locMngr;
	private MyLocationListener m_locLstrn;

	   @Override
	    public void onStart()
	    {	
	    	super.onStart();	    	
	    	
	        m_locMngr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	        m_locLstrn = new MyLocationListener();
	        m_locLstrn.act=this;
	    }
	    /*#1*/
	    @Override
	    public void onResume()
	    {
	    	super.onResume();
	    	m_locMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, m_locLstrn);
	    }
	    @Override
	    public void onPause()
	    {
	    	super.onPause();
	    	m_locMngr.removeUpdates(m_locLstrn);
	    }
	    @Override
	    public void onStop()
	    {
	    	super.onStop();
	    	
	    }
	    public void onLocationChanged(int d, int e){
	    	//mise a jour de la carte
	    	mController = m_mapView.getController();
	        mController.animateTo(new GeoPoint( d, e));	          	        
	        OverlayItem itemPosition = new OverlayItem(new GeoPoint( d, e), "Item position", "Item position description");
	        
	        Drawable aMarker = getResources().getDrawable(R.drawable.smiley);
	        aMarker.setBounds(0,0,aMarker.getIntrinsicWidth(),aMarker.getIntrinsicHeight());
			itemPosition.setMarker(aMarker);

			
			if(images.size() == ListMarkersTresor.getInstance().getListMarkerTresors().size()){
				images.addOverlay(itemPosition);
			}else{
				images.set(itemPosition,images.size()-1);
			}
			
	        //recherche du markers le plus pres
	        int indice_markers = ListMarkersTresor.getInstance().getMarkerByPosition(d,e);
			if(indice_markers != -1  && indice_markers != lastIndiceMarkers)//ok
			{
				//recuperation du markers tresor proche en data
				MarkerTresor unTresorProche = ListMarkersTresor.getInstance().getListMarkerTresors().get(indice_markers);
				//mise a jour du markers en actif 
				unTresorProche.setActif(true);
				ListMarkersTresor.getInstance().getListMarkerTresors().set(indice_markers, unTresorProche);
				
				//recuperation du markers de la carte 
				OverlayItem itemPositionProche = new OverlayItem(new GeoPoint( unTresorProche.getLatitude(), unTresorProche.getLongitude()), "Item position Proche", "Item position le plus proche");
				images.set(itemPositionProche,indice_markers);	
				
				//changement de la couleur du marqueur		
				Drawable marker = getResources().getDrawable(R.drawable.green_dot);
				marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
			    itemPositionProche.setMarker(marker);				
				
				//mise  a jour de l'affichage de l'indice
				TextView textView = (TextView) findViewById(R.id.textClue);
				textView.setText("L'indice est  " + unTresorProche.getIndice());		
				TextView textView2 = (TextView) findViewById(R.id.textPosition);
				textView2.setText("");
				
				//remise a l'ancienne couleur de lancien marqueur
				if(lastIndiceMarkers != -1 && indice_markers != lastIndiceMarkers){
					MarkerTresor unTresorProcheOld = ListMarkersTresor.getInstance().getListMarkerTresors().get(lastIndiceMarkers);
					OverlayItem itemPositionProcheOld = new OverlayItem(new GeoPoint( unTresorProcheOld.getLatitude(), unTresorProcheOld.getLongitude()), "Item position Proche", "Item position le plus proche");
					images.set(itemPositionProcheOld,lastIndiceMarkers);	
					//changement de la couleur du marqueur		
					Drawable unMarker = getResources().getDrawable(R.drawable.yellow_dot);
					unMarker.setBounds(0,0,unMarker.getIntrinsicWidth(),unMarker.getIntrinsicHeight());
					itemPositionProcheOld.setMarker(unMarker);
				}		
				
				//sauvegarde de l'indice du markers
				lastIndiceMarkers = indice_markers;
			}
			else
			{			
				//effacement de l'indice
				TextView textView = (TextView) findViewById(R.id.textClue);
				textView.setText("");
				TextView textView2 = (TextView) findViewById(R.id.textPosition);
				textView2.setText("");
				
				//remise a l'ancienne couleur de lancien marqueur
				if(lastIndiceMarkers != -1){
					MarkerTresor deuxTresorProcheOld = ListMarkersTresor.getInstance().getListMarkerTresors().get(lastIndiceMarkers);
					OverlayItem deuxitemPositionProcheOld = new OverlayItem(new GeoPoint( deuxTresorProcheOld.getLatitude(), deuxTresorProcheOld.getLongitude()), "Item position Proche", "Item position le plus proche");
					images.set(deuxitemPositionProcheOld,lastIndiceMarkers);	
					//changement de la couleur du marqueur		
					Drawable deuxMarker = getResources().getDrawable(R.drawable.yellow_dot);
					deuxMarker.setBounds(0,0,deuxMarker.getIntrinsicWidth(),deuxMarker.getIntrinsicHeight());
					deuxitemPositionProcheOld.setMarker(deuxMarker);
				}		
				lastIndiceMarkers = -1;
			}
	        	
	    }
	 public void OnButtonRelsoveClick(View v){

		 Intent intent = new Intent(MyMapActivity.this,ResolveViewActivity.class);
	     startActivity(intent);		 
	 }
}